'use strict';

const Service = require('egg').Service;
const Mock = require('mockjs');

class ArticleService extends Service {
    // 获取文章列表
    async getList() {
        const {
            ctx,
            app
        } = this;
        // const results = await app.mysql.select('articles');
        // console.log(results);
        // results.forEach((item)=>{
        //     app.mysql.update('articles', {
        //         id:item.id,
        //         create_time:Date.now()
        //     });
        // })
        // return 'ok'
        const {
            pageSize,
            pageIndex,
            title,
            type,
            tag,
            state,
        } = ctx.query;
        let sql = `select * from articles where 1=1 `;
        let countSql = `select count(id) as total from articles where 1=1 `
        if (title) {
            sql += `and title like "%${title}%" `
            countSql += `and title like "%${title}%" `
        }
        if (type) {
            sql += `and type="${type}" `
            countSql += `and type="${type}" `
        }
        if (tag) {
            sql += `and tag="${tag}" `
            countSql += `and tag="${tag}" `
        }
        if (state) {
            sql += `and state="${state}" `
            countSql += `and state="${state}" `
        }
        if (pageSize && pageIndex) {
            sql += `limit ${(pageIndex-1)*pageSize},${pageSize} `
        }
        const res = await app.mysql.query(sql);
        const [{
            total
        }] = await app.mysql.query(countSql);
        return {
            data: res,
            total,
        }
        //[{yaer:"".data"[{month:"",data:[{},{}]},{}]},{},{}]
    }
    // 获取文章详情
    async getDetail() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const res = await app.mysql.get('articles', {
            id
        })
        // 让访问量+1
        await app.mysql.update('articles', {
            id,
            read_num: ++res.read_num
        })
        return res
    }
    // 更新文章点赞
    async likeArticle() {
        const {
            ctx,
            app
        } = this;
        const {
            id,
            likes
        } = ctx.query;
        const res = await app.mysql.update('articles', {
            id,
            likes
        })
        return res
    }
    // 删除文章
    async delArticle() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.request.body;
        const res = await app.mysql.delete('articles', {
            id
        });
        return res
    }
    // 新建文章
    async addArticle(){
        const {ctx,app} = this;
        const {
            title,
            text,
            content,
            type,
            tag,
            url,
            password,
            money,
            isComment,
            isHome,
            state
        } = ctx.request.body;
        const res = app.mysql.insert('articles',{
            title,
            state,
            type,
            text,
            content,
            read_num:0,
            likes:0,
            tag,
            url,
            create_time:Date.now(),
            password,
            money,
            isComment:Number(isComment),
            isHome:Number(isHome)
        });
        return res
    }
    // 添加分类
    async addClassify() {
        const {
            ctx,
            app
        } = this;
        const {
            name,
            value
        } = ctx.request.body;
        const info = await app.mysql.get('classifys', {
            name,
            value
        });
        if (info) {
            return '分类已存在'
        } else {
            const res = await app.mysql.insert('classifys', {
                name,
                value
            });
            return res
        }
    }
    // 获取分类列表
    async getClassifyList() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.query;
        if (id) {
            let res = await app.mysql.get('classifys', {
                id
            });
            const children = await app.mysql.select('articles', {
                where: {
                    type: res.name
                    // 2002年10月
                }
            })
            res.children = children;
            return res
        }
        // 获取分类列表
        let classifys = await app.mysql.select('classifys');
        // 创建一个存放promise的数组
        let promiseArr = [];
        // 循环classifys
        classifys.forEach((item) => {
            // 添加promise任务
            promiseArr.push(app.mysql.select('articles', {
                where: {
                    type: item.name
                }
            }))
        })
        // 执行所有promise任务
        await Promise.all(promiseArr).then(res => {
            // 根据结果给classifys每一个元素添加children
            res.forEach((item, index) => {
                classifys[index].children = item;
            })
        })
        return classifys
    }
    // 删除分类
    async delClassify() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.request.body;
        const res = await app.mysql.delete('classifys', {
            id
        });
        return res
    }
    // 编辑分类
    async editClassify() {
        const {
            ctx,
            app
        } = this;
        const {
            id,
            name,
            value
        } = ctx.request.body;
        const res = await app.mysql.update('classifys', {
            id,
            name,
            value
        });
        return res
    }

    // 添加标签
    async addTag() {
        const {
            ctx,
            app
        } = this;
        const {
            name,
            value
        } = ctx.request.body;
        const info = await app.mysql.get('tags', {
            name,
            value
        });
        if (info) {
            return '标签已存在'
        } else {
            const res = await app.mysql.insert('tags', {
                name,
                value
            });
            return res
        }
    }
    // 获取标签列表
    async getTagList() {
        const {
            ctx,
            app
        } = this;
        const {
            name
        } = ctx.query;
        if (name) {
            let res = await app.mysql.get('tags', {
                name
            });
            let children = await app.mysql.select('articles', {
                where: {
                    tag: res.name
                }
            });
            res.children = children;
            return res
        }
        // 获取标签列表
        let tags = await app.mysql.select('tags');
        // 创建一个存放promise的数组
        let promiseArr = [];
        // 循环tags
        tags.forEach((item) => {
            // 添加promise任务
            promiseArr.push(app.mysql.select('articles', {
                where: {
                    tag: item.name
                }
            }))
        })
        // 执行所有promise任务
        await Promise.all(promiseArr).then(res => {
            // 根据结果给tags每一个元素添加children
            res.forEach((item, index) => {
                tags[index].children = item;
            })
        })
        return tags
    }
    // 删除标签
    async delTag() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.request.body;
        const res = await app.mysql.delete('tags', {
            id
        });
        return res
    }
    // 编辑标签
    async editTag() {
        const {
            ctx,
            app
        } = this;
        const {
            id,
            name,
            value
        } = ctx.request.body;
        const res = await app.mysql.update('tags', {
            id,
            name,
            value
        });
        return res
    }
}

module.exports = ArticleService;